import { Component, OnChanges, OnInit, SimpleChanges, Input } from '@angular/core';
import { HttpServcie } from '../../../service/http.service';
import { Router } from '@angular/router';
import { NzNotificationService } from 'ng-zorro-antd';
import { Observable } from 'rxjs/Observable';

@Component({
    selector: 'superviser-safe',
    templateUrl: 'superviser-safe.html',
    styleUrls: ['superviser-safe.scss']
})

export class SuperviserSafe implements OnInit, OnChanges {

    constructor(
        private httpServcie: HttpServcie,
        private router: Router,
        private _notification: NzNotificationService
    ) {

    }
    @Input() partyId: string = "";

    usedPartyNum: any;
    maintPartyNum: any;
    feedBackNum: any;
    overCheckNum: any;
    option1 = {};
    option2 = {};
    option4 = {};
    option5 = {};
    option6 = {};
    option7 = {};
    option8 = {};

    rescueNum: any;
    drillNum: any;
    repairRio: any;
    maintInRio: any;
    rescueDrillCount: any;
    totalMember: number = 0;
    cztotalMember: number = 0;

    ngOnInit() {
        //Called after the constructor, initializing input properties, and the first call to ngOnChanges.
        //Add 'implements OnInit' to the class.

    }

    ngOnChanges(changes: SimpleChanges) {

        this.countMaint();              //维保准时率，各维保平均用时
        this.countRescue();            //救援次数
        this.countRescueDrill();        //救援演练次数
        this.countFeedbackYear();       //年度 乘客投诉次数
        this.countOvertimeElevator();    //检验超期电梯数量及比例

        this.countRescueNum();        //各类型 应急救援数量

    }

    countRescueNum() {
        let param = {
            id: this.partyId
        }
        this.httpServcie.countRescueNum(param).subscribe(data => {
            if (data.status == 200) {
                if (data.data && (<Array<any>>data.data).length) {
                    let nameList = [];
                    let memberList = [];
                    (<Array<any>>data.data).forEach(rescueOb => {
                        if (rescueOb.id == this.partyId && rescueOb.accident_reason) {
                            nameList.push(rescueOb.accident_reason);
                            memberList.push({
                                name: rescueOb.accident_reason,
                                value: rescueOb.rescue_count
                            });
                        }
                    });

                    this.option8 = {
                        title: {
                            text: '各类型应急救援占比',
                            subtext: '',
                            x: 'center'
                        },
                        tooltip: {
                            trigger: 'item',
                            formatter: "{a} <br/>{b} : {c} ({d}%)"
                        },
                        legend: {
                            orient: 'vertical',
                            left: 'left',
                            data: nameList
                        },
                        series: [
                            {
                                name: '次',
                                type: 'pie',
                                radius: '30%',
                                center: ['45%', '50%'],
                                data: memberList,
                                itemStyle: {
                                    emphasis: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };

                }
            }
        });
    }


    countOvertimeElevator() {
        let param = {
            id: this.partyId
        }
        this.httpServcie.countOvertimeElevator(param).subscribe(data => {

            if (data.status == 200) {
                let overCheckList = data.data;
                let nameList = [];
                let memberList = [];
                overCheckList.forEach(rescue => {
                    if (rescue.id == this.partyId) {
                        this.overCheckNum = rescue.over_ele_count;
                    } else {
                        let over_ele_count = rescue.over_ele_count;
                        memberList.push({
                            name: rescue.party_name,
                            value: over_ele_count
                        });
                        nameList.push(rescue.party_name);
                    }
                });

                this.option7 = {
                    title: {
                        text: '超时年检分布图',
                        subtext: '',
                        x: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: nameList
                    },
                    series: [
                        {
                            name: '台',
                            type: 'pie',
                            radius: '30%',
                            center: ['45%', '50%'],
                            data: memberList,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            }
        });
    }


    countFeedbackYear() {
        let param = {
            id: this.partyId
        }
        this.httpServcie.countFeedbackYear(param).subscribe(data => {
            if (data.status == 200) {
                let feedList = data.data;
                let nameList = [];
                let memberList = [];
                feedList.forEach(feed => {
                    if (feed.id == this.partyId) {
                        this.feedBackNum = feed.feed_count;
                    } else {
                        let feed_count = feed.feed_count;
                        memberList.push({
                            name: feed.party_name,
                            value: feed_count
                        });
                        nameList.push(feed.party_name);
                    }
                });

                this.option6 = {
                    title: {
                        text: '投诉次数分布图',
                        subtext: '',
                        x: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: nameList
                    },
                    series: [
                        {
                            name: '次',
                            type: 'pie',
                            radius: '30%',
                            center: ['45%', '50%'],
                            data: memberList,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            }

        });

    }

    countRescue() {
        let param = {
            id: this.partyId
        }
        this.httpServcie.countRescue(param).subscribe(data => {
            if (data.status == 200) {
                let rescueList = data.data;
                let nameList = [];
                let memberList = [];
                rescueList.forEach(rescue => {
                    if (rescue.id == this.partyId) {
                        this.rescueNum = rescue.rescue_count;
                    } else {
                        let rescue_count = rescue.rescue_count;
                        if (!rescue.rescue_count) {
                            rescue_count = 0;
                        }
                        memberList.push({
                            name: rescue.party_name,
                            value: rescue_count
                        });
                        nameList.push(rescue.party_name);
                    }
                });

                this.option1 = {
                    title: {
                        text: '应急救援次数图',
                        subtext: '',
                        x: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: nameList
                    },
                    series: [
                        {
                            name: '次数',
                            type: 'pie',
                            radius: '30%',
                            center: ['45%', '50%'],
                            data: memberList,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };

            } else if (data.status === 401) {
                this.router.navigateByUrl('/login');
                return;
            } else {
                // 异常
                this._notification.create('error', '错误提示', data.message);
            }

        })
    }

    countRescueDrill() {
        let param = {
            id: this.partyId
        }
        this.httpServcie.countRescueDrill(param).subscribe(data => {
            if (data.status == 200) {
                let rescueDrillList = data.data;

                let nameList = [];
                let memberList = [];
                rescueDrillList.forEach(rescueDrill => {
                    if (rescueDrill.id == this.partyId) {
                        this.rescueDrillCount = rescueDrill.drill_count;
                    } else {
                        memberList.push({
                            name: rescueDrill.party_name,
                            value: rescueDrill.drill_count
                        });
                        nameList.push(rescueDrill.party_name);
                    }
                });

                this.option2 = {
                    title: {
                        text: '救援演练次数图',
                        subtext: '',
                        x: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: nameList
                    },
                    series: [
                        {
                            name: '次数',
                            type: 'pie',
                            radius: '30%',
                            center: ['45%', '50%'],
                            data: memberList,
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
            }

        })
    }


    countMaint() {
        let param = {
            id: this.partyId
        }
        this.httpServcie.countMaint(param).subscribe(data => {
            if (data.status == 200) {
                let maintIntimeList = data.data.maintInTime;
                let nameList = [];
                let memberList = [];
                maintIntimeList.forEach(maintInTime => {
                    if (maintInTime.id == this.partyId) {
                        this.maintInRio = (maintInTime.maint_rio * 100) + '%';
                    } else {
                        nameList.push(maintInTime.party_name);
                        memberList.push({
                            name: maintInTime.party_name,
                            value: (maintInTime.maint_rio * 100)
                        });
                    }
                });

                this.option4 = {
                    title: {
                        text: '维保准时率图表',
                        subtext: '',
                        x: 'center'
                    },
                    color: ['#3398DB'],
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: nameList,
                            axisTick: {
                                alignWithLabel: true
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '%'
                        }
                    ],
                    series: [
                        {
                            name: '准时率',
                            type: 'bar',
                            barWidth: '15',
                            data: memberList //[10, 52, 200, 334, 390, 330, 220]
                        }
                    ]
                };




                let maintTimeList = data.data.maintTime;
                let dataList = [0, 0, 0, 0];
                maintTimeList.forEach(maintTime => {
                    if (maintTime.id == this.partyId) {
                        if (maintTime.type == 1) {
                            dataList[0] = maintTime.maint_used_time;
                        }
                        if (maintTime.type == 2) {
                            dataList[1] = maintTime.maint_used_time;
                        }
                        if (maintTime.type == 3) {
                            dataList[2] = maintTime.maint_used_time;
                        }
                        if (maintTime.type == 4) {
                            dataList[3] = maintTime.maint_used_time;
                        }
                    }
                });

                this.option5 = {
                    title: {
                        text: '维保耗时图表',
                        subtext: '',
                        x: 'center'
                    },
                    color: ['#3398DB'],
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: ['半月保', '季度保', '半年保', '年保'],
                            axisTick: {
                                alignWithLabel: true
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '分钟'
                        }
                    ],
                    series: [
                        {
                            name: '平均时间',
                            type: 'bar',
                            barWidth: '15',
                            data: dataList //[10, 52, 200, 334, 390, 330, 220]
                        }
                    ]
                };


            }


        })
    }


}
